00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020 #ifndef _task_manager_hpp_
00021 #define _task_manager_hpp_
00022
00023 #include "gridpack/parallel/communicator.hpp"
00024 #include <ga.h>
00025
00026 namespace gridpack {
00027 namespace parallel {
00028
00029
00030
00031
00032 class TaskManager {
00033 public:
00034
00035
00036
00037
00038 TaskManager(void)
00039 {
00040 p_grp = GA_Pgroup_get_world();
00041
00042 p_GAcounter = GA_Create_handle();
00043 int one = 1;
00044 GA_Set_data(p_GAcounter,one,&one,C_INT);
00045 GA_Set_pgroup(p_GAcounter,p_grp);
00046 if (!GA_Allocate(p_GAcounter)) {
00047
00048 }
00049 GA_Zero(p_GAcounter);
00050 p_ntasks = 0;
00051 }
00052
00053
00054
00055
00056
00057 TaskManager(Communicator &comm)
00058 {
00059 p_grp = comm.getGroup();
00060
00061 p_GAcounter = GA_Create_handle();
00062 int one = 1;
00063 GA_Set_data(p_GAcounter,one,&one,C_INT);
00064 GA_Set_pgroup(p_GAcounter,p_grp);
00065 if (!GA_Allocate(p_GAcounter)) {
00066
00067 }
00068 GA_Zero(p_GAcounter);
00069 p_ntasks = 0;
00070 }
00071
00072
00073
00074
00075 ~TaskManager(void)
00076 {
00077 GA_Destroy(p_GAcounter);
00078 }
00079
00080
00081
00082
00083
00084 void set(int ntasks)
00085 {
00086 GA_Zero(p_GAcounter);
00087 p_ntasks = ntasks;
00088 p_task_count = 0;
00089 }
00090
00091
00092
00093
00094
00095
00096
00097
00098 bool nextTask(int *next) {
00099 int zero = 0;
00100 long one = 1;
00101 *next = static_cast<int>(NGA_Read_inc(p_GAcounter,&zero,one));
00102 if (*next < p_ntasks) {
00103 p_task_count++;
00104 return true;
00105 } else {
00106 *next = -1;
00107 GA_Pgroup_sync(p_grp);
00108 return false;
00109 }
00110 }
00111
00112
00113
00114
00115
00116
00117
00118
00119
00120
00121
00122 bool nextTask(Communicator &comm, int *next) {
00123 int zero = 0;
00124 long one = 1;
00125 int me = comm.rank();
00126 if (me == 0) {
00127 *next = static_cast<int>(NGA_Read_inc(p_GAcounter,&zero,one));
00128 } else {
00129 *next = 0;
00130 }
00131 char plus[2];
00132 strcpy(plus,"+");
00133 GA_Pgroup_igop(comm.getGroup(),next,one,plus);
00134 if (*next < p_ntasks) {
00135 p_task_count++;
00136 return true;
00137 } else {
00138 *next = -1;
00139 GA_Pgroup_sync(p_grp);
00140 return false;
00141 }
00142 }
00143
00144
00145
00146
00147
00148
00149
00150 void cancel(void) {
00151 int zero = 0;
00152 int n = static_cast<int>(NGA_Read_inc(p_GAcounter,&zero, p_ntasks));
00153 }
00154
00155
00156
00157
00158 void printStats() {
00159 int nprocs = GA_Pgroup_nnodes(p_grp);
00160 int me = GA_Pgroup_nodeid(p_grp);
00161 std::vector<int> procs(nprocs);
00162 int i;
00163 for (i=0; i<nprocs; i++) procs[i] = 0;
00164 procs[GA_Pgroup_nodeid(p_grp)] = p_task_count;
00165 char plus[2];
00166 strcpy(plus,"+");
00167 GA_Pgroup_igop(p_grp,&(procs[0]),nprocs,plus);
00168
00169 if (me == 0) {
00170 printf("\nNumber of tasks per processors\n");
00171 for (i=0; i<nprocs; i++) {
00172 printf(" Number of tasks on process %6d: %6d\n",i,procs[i]);
00173 }
00174 }
00175 }
00176
00177 protected:
00178
00179 int p_GAcounter;
00180 int p_ntasks;
00181 int p_grp;
00182 int p_task_count;
00183 };
00184
00185
00186 }
00187 }
00188
00189 #endif
00190